package com.demodis.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.demodis.dao.GroupDaoI;
import com.demodis.dao.ModelDaoI;
import com.demodis.dao.PermissionDaoI;
import com.demodis.dao.RoleDaoI;
import com.demodis.dao.UserDaoI;
import com.demodis.dao.impl.GroupDaoImpl;
import com.demodis.dao.impl.ModelDaoImpl;
import com.demodis.dao.impl.PermissionDaoImpl;
import com.demodis.dao.impl.RoleDaoImpl;
import com.demodis.dao.impl.UserDaoImpl;
import com.demodis.domain.TblGroup;
import com.demodis.domain.TblModel;
import com.demodis.domain.TblPermission;
import com.demodis.domain.TblRole;
import com.demodis.domain.TblUser;

public class InitApp extends HttpServlet {

	private static final long serialVersionUID = -3600237359068869370L;
	private static Logger log = Logger.getLogger(InitApp.class);
	GroupDaoI groupDao = new GroupDaoImpl();
	UserDaoI userDao = new UserDaoImpl();
	Map<String, String> groupM = new HashMap<String, String>();

	@Override
	public void init(){
		try {
			super.init();
			// new BaseDao().initUser();
			// new DeterGroupServiceImpl().initTblDeterPerson();
//			Utils.truncate();
//			initTblGroup();
//			initTblUser();
//			initTblModel();
//			initTblPermission();
//			initTblRole();
		} catch (Exception e) {
			log.info("数据初始化失败！");
			e.printStackTrace();
		}
	}

	public void initTblGroup() throws Exception {
		HSSFSheet groupSheet = Utils.getSheet("tbl_group.xls");
		TblGroup group = new TblGroup();
		for (int i = 1; i <= groupSheet.getLastRowNum(); i++) {
			HSSFRow row = groupSheet.getRow(i);

			String groupName = row.getCell(0).getStringCellValue();
			int showSort = (int) row.getCell(1).getNumericCellValue();
			group.setGroupName(groupName);
			group.setShowSort(showSort);
			groupM.put(group.getGroupName(), row.getCell(2).getStringCellValue());
			groupDao.save(group);
		}
		group.setGroupName("系统管理员");
		group.setShowSort(100);
		groupDao.save(group);
		groupM.put("系统管理员", "admin");
		log.info("TblGroup表基础数据初始化完毕！");
	}

	public void initTblUser() throws Exception {
		HSSFSheet userSheet = Utils.getSheet("tbl_user.xls");
		List<TblGroup> groupList = groupDao.getGroup("");
		Map<String, Integer> groupMap = new HashMap<String, Integer>();
		Map<String, Integer> userMap = new HashMap<String, Integer>();

		for (TblGroup g : groupList) {
			groupMap.put(g.getGroupName(), g.getGroupId());
		}

		TblUser user;
		for (int i = 1; i <= userSheet.getLastRowNum(); i++) {
			user = new TblUser();
			HSSFRow row = userSheet.getRow(i);

			user.setUsername(row.getCell(0).getStringCellValue());
			user.setUserRealName(row.getCell(1).getStringCellValue());
			user.setGroupId(groupMap.get(row.getCell(2).getStringCellValue()));
			user.setPassword(row.getCell(3).getStringCellValue());
			userDao.save(user);
		}
		user = new TblUser();
		user.setUsername("admin");
		user.setUserRealName("admin");
		user.setGroupId(groupMap.get("系统管理员"));
		user.setPassword(Encrypt.e("admin"));
		userDao.save(user);
		log.info("TblUser基础数据初始化完毕");

		List<TblUser> userList = userDao.getUsers();
		for (TblUser u : userList) {
			userMap.put(u.getUserRealName(), u.getUserId());
		}

		for (TblGroup g : groupList) {
			g.setGroupLeader(groupM.get(g.getGroupName()));
			g.setGroupLeaderId(userMap.get(groupM.get(g.getGroupName())));
			groupDao.modify(g);
		}
		log.info("TblGroup表基础信息维护完毕！");
	}
	
	public void initTblModel() throws Exception {
		HSSFSheet modelSheet = Utils.getSheet("tbl_model.xls");
		
		ModelDaoI modelDao = new ModelDaoImpl();
		TblModel model = null;
		
		for (int i = 1; i <= modelSheet.getLastRowNum(); i++) {
			model = new TblModel();
			HSSFRow row = modelSheet.getRow(i);

			model.setModName(row.getCell(0).getStringCellValue());
			model.setModDes(row.getCell(1).getStringCellValue());
			model.setParMod((int) row.getCell(2).getNumericCellValue());
			model.setModCd((int) row.getCell(3).getNumericCellValue());
			HSSFCell modUrlCell = row.getCell(4);
			if(modUrlCell != null) {
				model.setModUrl(modUrlCell.getStringCellValue());
			}
			
			modelDao.save(model);
		}
		log.info("TblModel表基础信息初始化完毕！");
	}

	public void initTblPermission() throws Exception {
		HSSFSheet modelSheet = Utils.getSheet("tbl_permission.xls");
				
		PermissionDaoI permDao = new PermissionDaoImpl();
		TblPermission perm = null;
		
		for (int i = 1; i <= modelSheet.getLastRowNum(); i++) {
			perm = new TblPermission();
			HSSFRow row = modelSheet.getRow(i);

			perm.setPermId(UUID.randomUUID().toString());
			perm.setPermName(row.getCell(0).getStringCellValue());
			perm.setPermUrl(row.getCell(1).getStringCellValue());
			perm.setModId((int) row.getCell(2).getNumericCellValue());
			
			permDao.save(perm);
		}
		log.info("TblPermission表基础信息初始化完毕！");
	}
	
	public void initTblRole() throws Exception {
		HSSFSheet modelSheet = Utils.getSheet("tbl_role.xls");
		
		RoleDaoI roleDao = new RoleDaoImpl();
		TblRole role = null;
		
		for (int i = 1; i <= modelSheet.getLastRowNum(); i++) {
			role = new TblRole();
			HSSFRow row = modelSheet.getRow(i);

			role.setRoleName(row.getCell(0).getStringCellValue());
			role.setRoleDes(row.getCell(1).getStringCellValue());
			
			roleDao.save(role);
		}
		log.info("TblRole表基础信息初始化完毕！");
	}
	
}
